package myta.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.model.SelectItem;

import myta.base.FormDataBase;
import myta.model.DataRow;
import myta.model.DataTable;
import myta.service.Command;
import myta.service.SQLexec;

public class KelasData extends FormDataBase{
	private HtmlInputText txtid;
	private HtmlInputText txtnama;
	private HtmlSelectOneMenu txtidsemester;
	private HtmlSelectOneMenu txtidjurusan;
	
	private List<SelectItem> listSemester;
	private List<SelectItem> listJurusan;
	
	@Override
	public void init() throws Exception {
		formName = "kelas";
		
		txtid = new HtmlInputText();
		txtnama = new HtmlInputText();
		txtidjurusan = new HtmlSelectOneMenu();
		txtidsemester = new HtmlSelectOneMenu();
		
		loadList();
	}
	
	private void loadList() throws Exception{
		listJurusan = new ArrayList<SelectItem>();
		listSemester = new ArrayList<SelectItem>();
		
		SelectItem s = new SelectItem();
		s.setValue("-");
		s.setLabel("-");
		
		listJurusan.add(s);
		
		String sql = "select idsemester, concat(semester,' ~ ',thnajaran) as semester from semester order by idsemester";
		Command command = SQLexec.makeCommand(sql);
		DataTable dt = command.executeQuery();
		
		for(int i=0;i<dt.getRowCount();i++){
			DataRow dr = dt.getRow(i);
			
			s = new SelectItem();
			s.setValue(dr.getString("idsemester"));
			s.setLabel(dr.getString("semester"));
			
			listSemester.add(s);
		}
		
		sql = "select * from jurusan order by idjurusan";
		command = SQLexec.makeCommand(sql);
		dt = command.executeQuery();
		
		for(int i=0;i<dt.getRowCount();i++){
			DataRow dr = dt.getRow(i);
			
			s = new SelectItem();
			s.setValue(dr.getString("idjurusan"));
			s.setLabel(dr.getString("namajurusan"));
			
			listJurusan.add(s);
		}
	}

	@Override
	public void generateID() throws Exception {
		String sql = "call idkelas()";
		Command command = SQLexec.makeCommand(sql);
		DataTable datatable = command.executeQuery();
		DataRow datarow = datatable.getRow(0);
		txtid.setValue(datarow.getString("id"));
	}

	@Override
	public void getRequestData(Map<String, String> map) throws Exception {
		if(map.containsKey("idkelas")){
			String idkelas = map.get("idkelas");
			
			String sql = "select ";
			sql += "k.idkelas,k.namakelas,s.idsemester,s.semester,j.idjurusan,j.namajurusan "; 
			sql += "from kelas k  ";
			sql += "inner join semester s on s.idsemester = k.idsemester ";
			sql += "inner join jurusan j on k.idjurusan =  j.idjurusan";
			sql += "where k.idkelas = ?";
			
			Command command = SQLexec.makeCommand(sql);
			command.setObject(1, idkelas);
			DataTable datatable = command.executeQuery();
			DataRow datarow = datatable.getRow(0);
			
			txtid.setValue(datarow.getString("idkelas"));
			txtnama.setValue(datarow.getString("namakelas"));
			txtidsemester.setValue(datarow.getString("idsemester"));
			txtidjurusan.setValue(datarow.getString("idjurusan"));
		}
	}

	@Override
	public boolean simpan() throws Exception {
		boolean boo = false;
		
		String sql = "INSERT INTO kelas(idkelas,namakelas,idjurusan,idsemester) VALUES(?,?,?,?);";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtid.getValue().toString());
		command.setObject(2, txtnama.getValue().toString());
		command.setObject(3, txtidjurusan.getValue().toString());
		command.setObject(4, txtidsemester.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}		
	}

	@Override
	public boolean ubah() throws Exception {
		boolean boo = false;
		
		String sql = "UPDATE kelas SET namakelas=?,idsemester=?,idjurusan=? where idkelas = ?;";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtnama.getValue().toString());
		command.setObject(2, txtidsemester.getValue().toString());
		command.setObject(3, txtidjurusan.getValue().toString());
		command.setObject(4, txtid.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}
	}

	public HtmlInputText getTxtid() {
		return txtid;
	}

	public void setTxtid(HtmlInputText txtid) {
		this.txtid = txtid;
	}

	public HtmlInputText getTxtnama() {
		return txtnama;
	}

	public void setTxtnama(HtmlInputText txtnama) {
		this.txtnama = txtnama;
	}

	public HtmlSelectOneMenu getTxtidsemester() {
		return txtidsemester;
	}

	public void setTxtidsemester(HtmlSelectOneMenu txtidsemester) {
		this.txtidsemester = txtidsemester;
	}

	public HtmlSelectOneMenu getTxtidjurusan() {
		return txtidjurusan;
	}

	public void setTxtidjurusan(HtmlSelectOneMenu txtidjurusan) {
		this.txtidjurusan = txtidjurusan;
	}



	public List<SelectItem> getListSemester() {
		return listSemester;
	}



	public void setListSemester(List<SelectItem> listSemester) {
		this.listSemester = listSemester;
	}



	public List<SelectItem> getListJurusan() {
		return listJurusan;
	}



	public void setListJurusan(List<SelectItem> listJurusan) {
		this.listJurusan = listJurusan;
	}
}
